package com.kiln.juxingqs.ui.vm

import android.app.Application
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.viewModelScope
import client.global.commlib.network.data.JsonResult
import client.global.commlib.network.data.Ret
import client.global.commlib.out_time
import client.global.commlib.toast.toast
import com.kiln.juxingqs.api.bean.UserInfo
import com.kiln.juxingqs.ui.viewmodel.MiddleVm
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.flow.*
import kotlinx.coroutines.launch
import retrofit2.Response

class ZzViewModel(app: Application) : MiddleVm(app) {
    val call = MutableLiveData<JsonResult<Ret>>()
    val isShowing = MutableLiveData<Boolean>()
    val phone = MutableLiveData<String>("")
    val username = MutableLiveData<String>("")
    val txNum = MutableLiveData<String>()
    val userInfo = MutableLiveData<UserInfo>()

    fun okHuaZhuan(goods_id: String, it: String) {
        isShowing.value = true
        viewModelScope.launch {
            flow {
                var emm: Response<JsonResult<Ret>>? = null
                out_time {
                    val ret = server.validatePayPwd(it)
                    emm = ret
                }

                emit(emm)
            }.map {
                var emm: Response<JsonResult<Ret>>? = it
                it?.body()?.let {
                    if (it.isSuccess) {
                        out_time {
                            val ret = server.transfer_ticket(goods_id,
                                phone.value!!,
//                                username.value!!,
                                txNum.value!!
                            )
                            emm = ret
                        }
                    }
                }
                emm
            }.flowOn(Dispatchers.IO)
                .onCompletion {
                    isShowing.value = false
                }.catch { cause ->
                    if (cause != null) {
                        toast(cause.message)
                    }
                }.collectLatest {
                    call.value = it?.body()
                }
        }
    }


//    fun ok(it: String) {
//        isShowing.value = true
//        viewModelScope.launch {
//            flow {
//                var emm: Response<JsonResult<Ret>>? = null
//                out_time {
//                    val ret = server.validatePayPwd(it)
//                    emm = ret
//                }
//
//                emit(emm)
//            }.map {
//                var emm: Response<JsonResult<Ret>>? = it
//                it?.body()?.let {
//                    if (it.isSuccess) {
//                        out_time {
//                            val ret = server.transfer(
//                                username.value!!,
//                                txNum.value!!
//                            )
//                            emm = ret
//                        }
//                    }
//                }
//                emm
//            }.flowOn(Dispatchers.IO)
//                .onCompletion {
//                    isShowing.value = false
//                }.catch { cause ->
//                    if (cause != null) {
//                        toast(cause.message)
//                    }
//                }.collectLatest {
//                    call.value = it?.body()
//
//                }
//        }
//    }

}